import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import AddEditModal from '#cpt/temp/AddEditModal'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 				= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 			= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		// 搜索表单列表
		forms : [
			{ label: '人员姓名',		name: 'real_name' },
			{ label: '所属区域',		name: 'region_name',	type: 'select', loading:true },
			{ label: '在职状态',		name: 'status', type:'select',data:[{name:'在职',value:'1'},{name:'离职',value:'-1'}] },
		],
		// 提交表单列表
		formData : [
			{ label: '真实姓名',		name: 'salesman_id', 	half:true, 	required:true,	type: 'select', loading:true, onValue: ({ rows, ref })=>{
				ref.setValue(rows)
			} },
			{ label: '籍贯',			name: 'address', 		half:true },
			{ label: '入职时间',		name: 'entrytime', 		half:true, 	required:true, 	type:'date-time' },
			{ label: '员工编码',		name: 'number', 		half:true, 	required:true },
			{ label: '年龄',			name: 'age', 			half:true, 	required:true  },
			{ label: '手机号码',		name: 'phone', 			half:true, 	required:true  },
			{ label: '在职状态',		name: 'status', 		half:true, 	required:true,	type:'select', nameStr:'name',data:[{name:'在职',value:'1'},{name:'离职',value:'-1'}] },
			{ label: '银行账号',		name: 'bank_account', 	half:true },
			{ label: '开户行',		name: 'open_bank', 		half:true },
			{ label: '最高学历',		name: 'education', 		half:true },
			{ label: '所属区域',		name: 'area_id', 		half:true,	type: 'select', loading:true, 	required:true },
		]
	}
	
	model = {}
	
	componentDidMount(){
		this.getSelect()
		
		if(window.isInit){
			this.fetch()
		}
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		$cache.then( async f => {
			// 区域
			$fn.setRow(forms, 'region_name', await f.getCache({ api:'bs_area' } ) )
			
			this.setState({ forms })
		})
	}
	// 获取弹窗下拉框数据
	getModalSelect = async () => {
		const { formData } = this.state
		$fn.setModalRows(formData, [ 'salesman_id', 'area_id' ], ()=>{
			$cache.then( async f => {
				// 真实姓名
				$fn.setRow(formData, 'salesman_id', await f.getCache({ api:'employee' } ) )
				// 区域
				$fn.setRow(formData, 'area_id', await f.getCache({ api:'bs_area',refresh:true } ) )
				//
				this.setState({ formData })
			})
		})
	}
	// paging
	fetch = () => $fn.fetch.call(this,'bs-salesman/index')
	
	// 表单
	cols = [
		{ title: '序号',		dataIndex: 'id', 			...$fn.table.num },
		{ title: '员工编码', 	dataIndex: 'empl_name', 	width:150},
		{ title: '真实姓名', 	dataIndex: 'real_name', 	...$fn.table.name },
		{ title: '年龄', 		dataIndex: 'age', 			...$fn.table.age },
		{ title: '籍贯',		dataIndex: 'address', 		width:200 },
		{ title: '最高学历', 	dataIndex: 'education', 	width:150},
		{ title: '手机号', 		dataIndex: 'phone', 		...$fn.table.phone },
		{ title: '所属区域', 	dataIndex: 'region_name', 	width:150},
		{ title: '在职状态', 	dataIndex: 'status', 		width:100, render: text => +text === 1 ? '在职' : '离职' },
		TableControls.controls.call(this, { width: 150 })
	]
	// 表格操作按钮
	table_controls = (text,{ uuid, salesman_id, region_id }) => {
		return [
			TableControls.edit.call(this, 'bs-salesman/info', {uuid}, {
				select: this.getModalSelect,
				click: ()=> $fn.disabledRows(this.state.formData, ['entrytime', 'number', 'age', 'phone'], false)
			}),
			TableControls.pushTo.call(this, '绑定医院',  `bind-hospital?region_id=${region_id}&my_salesman_id=${salesman_id}` )
		]
	}
	// 操作按钮
	controls = () => [
		Controls.add.call(this, { click: ()=>{
			const { formData } = this.state
			this.getModalSelect()
			$fn.disabledRows(formData, ['entrytime', 'number', 'age', 'phone'], true)
			this.setState({ formData })
		} }),
	]
	
	render(){
		return (
			<>
				<Page title='业务员管理' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this, { isInit: window.isInit }) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ labelWidth:'s6', width:'50%', minWidth:800, api:'bs-salesman/add', editApi: 'bs-salesman/edit' }) }
			</>
		)
	}
}

export default Index